package com.jm.weibo.ui;

import java.util.HashMap;
import java.util.List;

import weibo4andriod.Comment;
import weibo4andriod.Status;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.jm.weibo.logic.IWeiboActivity;
import com.jm.weibo.logic.ListCommentAdapter;
import com.jm.weibo.logic.MainService;
import com.jm.weibo.logic.Task;
import com.jm.weibo.util.AppContainer;

public class ListCommentsActivity extends Activity implements IWeiboActivity, OnClickListener, Runnable{
    
	//返回
	public static final int COME_BACK = 1;
	
	//发布评论
	public static final int POST_COMMENT = 2;
	
	private Button btnBack;
	
	private Button  btnComment;
	
	//查看的该微博的评论
	private Status status;
	
	private ListView listView;
	
	private View process;
	
	private List<Comment> commentList;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//取消标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		 
		HashMap hm=new HashMap();
		Task tsListComment=new Task(Task.TASK_GET_WEIBO_COMMENT_LIST,hm);
	    MainService.addNewTask(tsListComment);
		setContentView(R.layout.lsit_comments);
		
		MainService.allActivity.add(this);
		init();
	}
	
	public void init() {

		View titleview=this.findViewById(R.id.list_comment_title);
		this.btnBack = (Button) titleview.findViewById(R.id.title_bt_left);
	    //返回
		this.btnBack.setText(R.string.imageviewer_back);
		
		//评论
		this.btnComment = (Button) titleview.findViewById(R.id.title_bt_right);
		this.btnComment.setText(R.string.title_btn_comment);
		this.btnBack.setTag(COME_BACK);
		this.btnComment.setTag(POST_COMMENT);
		
		
		TextView tvTitle = ((TextView) titleview.findViewById(R.id.textView));
		tvTitle.setText(R.string.title_btn_comment); 
		
		//为返回和发布添加事件
		this.btnBack.setOnClickListener(this);
		this.btnComment.setOnClickListener(this);
		
		this.listView = (ListView) this.findViewById(R.id.comment_listview);
		  //获取列表对象和进度条对象
	    process = this.findViewById(R.id.progress4ListComment);
	     
	    //启动一个线程加载数据
	    Thread t = new Thread(this);
        t.start();
	    process.setVisibility(View.VISIBLE);
	    
	}

	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		
	}
   
	/**
	 * 事件的处理
	 * @param v 视图(View)
	 */
	public void onClick(View v) {
		 int tag = (Integer) v.getTag();
		 switch (tag) {
			case ListCommentsActivity.COME_BACK:
				this.finish();
				break;
				
			case ListCommentsActivity.POST_COMMENT:
				Intent intent = new Intent();
				intent.setClass(this, CommentActivity.class);
				Bundle bundle = new Bundle();
				bundle.putLong("id", status.getId());
				startActivity(intent);
				break;
			default:
				break;
		}
	}

	/**
	 * 用线程加载数据
	 */
	public void run() {
		System.out.println("-----------run----------");
	    this.commentList = AppContainer.commentsList;
	    Message msg = myHandler.obtainMessage();
	    myHandler.sendMessage(msg);
	}
   
	private Handler myHandler = new Handler(){
		
		public void handleMessage(android.os.Message msg) {
		    System.out.println("-----------handleMessage-----------");
			ListCommentsActivity.this.listView.setAdapter(new ListCommentAdapter(ListCommentsActivity.this, ListCommentsActivity.this.commentList));
			ListCommentsActivity.this.process.setVisibility(View.GONE);
		}
	};
}
